package com.zhz.stock.service.impl;

import com.zhz.mq.util.IdWorker;
import com.zhz.stock.dao.StockFlowMapper;
import com.zhz.stock.dao.StockMapper;
import com.zhz.stock.pojo.StockFlow;
import com.zhz.stock.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class StockServiceImpl implements StockService {
    
    @Autowired
    private StockMapper stockMapper;
    
    @Autowired
    private StockFlowMapper stockFlowMapper;
    
    @Autowired
    private IdWorker idWorker;
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int reduceStock(String goodsId, String messageId) {
        
        StockFlow stockFlow = new StockFlow();
        stockFlow.setId(String.valueOf(idWorker.nextId()));
        stockFlow.setNum(1);
        stockFlow.setGoodsId(goodsId);
        stockFlow.setFlag(messageId);
        stockFlowMapper.insert(stockFlow);
        return stockMapper.reduceStock(goodsId, 1);
        
    }
}
